/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package samples.fop;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;
import org.junit.Assert;
import org.junit.Test;
import org.retabel.RestServiceUtil;
import org.retabel.ZipArchiveBuilder;
import org.retabel.persistence.entity.report.RenderingEngineEnum;
import org.retabel.web.rest.RenderServiceBean;
import org.retabel.web.rest.ReportDesignPackageServiceBean;
import samples.util.RenderUtils;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

/**
 * Render a document with Apache FOP.
 *
 * This is a very basic example without any specials like images, fonts, ...
 *
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 23:51 22.06.11
 *
 * @author Peter Herber
 * @version 1.0
 */
public class Simple {


    @Test
    public void render() throws Exception {
        // upload the report design package first
        String newReportDesignPackageName = "fop.simple";
        String newReportDesignPackageVersion = "1.0";

        Client client = Client.create();
        UriBuilder uriBuilder = RestServiceUtil.getRestInterfaceUriBuilder().path(ReportDesignPackageServiceBean.class)
                                               .queryParam("archiveName", newReportDesignPackageName).queryParam("version",                                                                                                                 newReportDesignPackageVersion)
                                               .queryParam("renderingEngine", RenderingEngineEnum.FOP);
        WebResource webResource = client.resource(uriBuilder.build());

        byte[] reportDesignPackage = RenderUtils.getResourceAsByteArray("simple/reportdesign.zip", getClass());

        ClientResponse response = webResource.entity(reportDesignPackage, "application/zip").post(
            ClientResponse.class);

        Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());

        // render the document
        uriBuilder = RestServiceUtil.getRestInterfaceUriBuilder().path(RenderServiceBean.class);

        String dataFileInZip = "data.xml";
        String documentConfigurationFileInZip = "config.xml";

        ZipArchiveBuilder zipArchiveBuilder = new ZipArchiveBuilder();
        zipArchiveBuilder.addResource(dataFileInZip, "simple/data.xml", getClass());
        zipArchiveBuilder.addResource(documentConfigurationFileInZip, "simple/document-config.xml", getClass());

        byte[] renderJob = zipArchiveBuilder.toByteArray();

        MultivaluedMap<String, String> params = new MultivaluedMapImpl();
        params.add("data", dataFileInZip);
        params.add("config", documentConfigurationFileInZip);
        webResource = client.resource(uriBuilder.build()).queryParams(params);

        response = webResource.type("application/zip").post(ClientResponse.class, renderJob);

        Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());

        String uniqueRenderedDocumentIdentifier = response.getHeaders().getFirst("X-unique-rendered-document-identifier");

        RenderUtils.assertRenderedDocument(uniqueRenderedDocumentIdentifier);
    }

}
